package com.identify.controller;

import com.identify.constant.JwtClaimsConstant;
import com.identify.pojos.dtos.AppUserLoginDto;
import com.identify.pojos.dtos.UserLoginDto;
import com.identify.pojos.dtos.UserRegisterDto;
import com.identify.pojos.entity.Result;
import com.identify.pojos.entity.User;
import com.identify.pojos.vos.UserLoginVo;
import com.identify.properties.JwtProperties;
import com.identify.service.UserService;
import com.identify.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户注册
     * @param dto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto dto){
        userService.register(dto);
        return Result.success();
    }

    /**
     * app登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public Result appLogin(@RequestBody AppUserLoginDto dto){
        //登录
        User user = userService.login(dto);

        //为用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        //构建vo返回对象
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .avatarUrl(user.getAvatarUrl())
                .username(user.getUsername())
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 测试用户方法
     * @return
     */
    @GetMapping("/test")
    public Result userTest(){
        return Result.success(userService.userTest());
    }

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @PostMapping("/wxLogin")
    public Result login(@RequestBody UserLoginDto dto) {

        //微信登录
        User user = userService.wxLogin(dto);

        //为微信用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        //构建vo返回对象
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .avatarUrl(user.getAvatarUrl())
                .username(user.getUsername())
                .build();

        return Result.success(userLoginVO);

    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/changeUserInfo")
    public Result changeUserInfo(@RequestBody User user){
    	return Result.success(userService.updateById(user));
    }
}
